package com.chen.dao;

import com.chen.model.Invoice;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface InvoiceDao {

    int addInvoice(Invoice invoice);

    @Select("select * from invoice ,contract ,goods where contract.contract_id=invoice.contract_id and invoice.goods_id=goods.goods_id;")
    @Results({
        @Result(id = true, column="invoice_id",property = "invoice_id" ),
        @Result(column="goods_price",property = "goods_price" ),
        @Result(column="goods_number",property = "goods_number" ),
        @Result(column="invoice_state",property = "invoice_state" ),
        @Result(column="invoice_logistics",property = "invoice_logistics" ),
        @Result(column="invoice_time",property = "invoice_time" ),
        @Result(column="delivery_company",property = "delivery_company" ),
        @Result(column="addressee",property = "addressee" ),
        @Result(column="address",property = "address" ),
        @Result(column="goods_id",property="goods",
                one=@One(select="com.chen.dao.GoodsDao.findGoodsById",fetchType= FetchType.EAGER)),
        @Result(column="contract_id",property="contract",
                one=@One(select="com.chen.dao.ContractDao.findContractById",fetchType= FetchType.EAGER)),
    })
     List<Invoice> findAllInvoice();

    int updateInvoice(Invoice invoice);

    @Select("select * from invoice ,contract ,goods where invoice.contract_id=contract.contract_id and invoice.goods_id=goods.goods_id and invoice.contract_id=contract.contract_id and invoice_id=#{invoice_id};")
    @Results({
            @Result(id = true, column="invoice_id",property = "invoice_id" ),
            @Result(column="goods_price",property = "goods_price" ),
            @Result(column="goods_number",property = "goods_number" ),
            @Result(column="invoice_state",property = "invoice_state" ),
            @Result(column="invoice_logistics",property = "invoice_logistics" ),
            @Result(column="invoice_time",property = "invoice_time" ),
            @Result(column="delivery_company",property = "delivery_company" ),
            @Result(column="addressee",property = "addressee" ),
            @Result(column="address",property = "address" ),
            @Result(column="goods_id",property="goods",
                    one=@One(select="com.chen.dao.GoodsDao.findGoodsById",fetchType= FetchType.EAGER)),
            @Result(column="contract_id",property="contract",
                    one=@One(select="com.chen.dao.ContractDao.findContractById",fetchType= FetchType.EAGER)),
    })
     Invoice findInvoiceById(Integer invoice_id);

    @Select("select * from invoice ,contract ,goods where invoice.contract_id=contract.contract_id and invoice.goods_id=goods.goods_id and invoice.contract_id=#{contract_id};")
    @Results({
            @Result(id = true, column="invoice_id",property = "invoice_id" ),
            @Result(column="goods_price",property = "goods_price" ),
            @Result(column="goods_number",property = "goods_number" ),
            @Result(column="invoice_state",property = "invoice_state" ),
            @Result(column="invoice_logistics",property = "invoice_logistics" ),
            @Result(column="invoice_time",property = "invoice_time" ),
            @Result(column="delivery_company",property = "delivery_company" ),
            @Result(column="addressee",property = "addressee" ),
            @Result(column="address",property = "address" ),
            @Result(column="goods_id",property="goods",
                    one=@One(select="com.chen.dao.GoodsDao.findGoodsById",fetchType= FetchType.EAGER)),
            @Result(column="contract_id",property="contract",
                    one=@One(select="com.chen.dao.ContractDao.findContractById",fetchType= FetchType.EAGER)),
    })
     List<Invoice> findInvoiceByContract(Integer contract_id);

    @Update("update invoice set delivery_company=#{delivery_company},addressee=#{addressee},address=#{address},invoice_logistics=#{invoice_logistics},invoice_state=#{invoice_state} where invoice_id=#{invoice_id}")
    int sendInvoice(Invoice invoice);

    @Update("update invoice set invoice_state=#{invoice_state} where invoice_id=#{invoice_id}")
    int toSendInvoice(Invoice invoice);


}
